package com.runchen.broadcastdetection.ui.activity.home;

import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.blankj.utilcode.util.StringUtils;
import com.runchen.broadcastdetection.R;
import com.runchen.broadcastdetection.databinding.ActivityMainBinding;
import com.runchen.broadcastdetection.entity.DeviceInfo;
import com.runchen.broadcastdetection.mvp.home.MainCovenant;
import com.runchen.broadcastdetection.mvp.home.MainPresenter;
import com.ruochen.common.base.BaseActivity;
import com.ruochen.common.base.BaseMvpActivity;
import com.ruochen.common.base.BasePage;

public class MainActivity extends BaseMvpActivity<ActivityMainBinding, MainPresenter> implements MainCovenant.MvpView {

    private AMap aMap;

    @Override
    protected void initView(Bundle savedInstanceState) {
        viewBinding.mapView.onCreate(savedInstanceState);// 此方法必须重写
        if (aMap == null) {
            aMap = viewBinding.mapView.getMap();
        }
        aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 卫星地图模式
        mvpPresenter.getDeviceList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewBinding.mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        viewBinding.mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        viewBinding.mapView.onSaveInstanceState(outState);
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewBinding.mapView.onDestroy();
    }

    @Override
    public void onGetDeviceListSuccess(BasePage<DeviceInfo> devicePage) {
        aMap.clear();
        if(devicePage.getPage() != null && devicePage.getPage().getList() != null
                && devicePage.getPage().getList().size() > 0){
            for(DeviceInfo device : devicePage.getPage().getList()) {
                if(!StringUtils.isTrimEmpty(device.getDeviceLatitude()) &&
                    !StringUtils.isTrimEmpty(device.getDeviceLongitude())){
                    MarkerOptions markerOption = new MarkerOptions();
                    markerOption.position(new LatLng(Double.parseDouble(device.getDeviceLatitude()),
                            Double.parseDouble(device.getDeviceLongitude())));
                    markerOption.draggable(true);//设置Marker可拖动
                    markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                            .decodeResource(getResources(), R.mipmap.ic_launcher)));
                    aMap.addMarker(markerOption);
                }
            }
        }else {
            showToast("暂未发现设备");
        }
    }

    @Override
    public void onGetDeviceListFailure(int code, String msg) {
        showToast(msg);
    }
}